package com.ponhu.ea2.stockcenter.provider.service.entity.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ponhu.ea2.stockcenter.provider.constants.CommonConstant;
import com.ponhu.ea2.stockcenter.provider.entity.StockWarehouseChangeSettings;
import com.ponhu.ea2.stockcenter.provider.mapper.StockWarehouseChangeSettingsMapper;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockWarehouseChangeSettingsService;
import com.ponhu.ea2.stockcenter.provider.support.service.RedisSupportService;
import com.ponhu.ea2.stockcenter.provider.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 仓库库存操作配置表服务实现类
 */
@Service
public class StockWarehouseChangeSettingsServiceImpl
        extends ServiceImpl<StockWarehouseChangeSettingsMapper, StockWarehouseChangeSettings>
        implements StockWarehouseChangeSettingsService {

    @Autowired
    private RedisSupportService redisSupportService;

    /**
     * 获取当前请求事件动态类型的配置，先从Redis缓存获取，再从数据库获取
     *
     * @param recordChannel
     * @param actionType
     * @return
     */
    @Override
    public StockWarehouseChangeSettings getWarehouseChangeSettingsByType(Integer recordChannel, Integer actionType) {

        // 构建缓存KEY
        String redisKey = RedisUtils.getRedisKey(CommonConstant.WAREHOUSE_CHANGE_SETTINGS,
                recordChannel.toString(), actionType.toString());

        // 首先从Redis缓存获取数据，
        // Redis中没有再由 supplier 函数提供数据，
        // 最后将数据重新缓存到Redis后返回
        return redisSupportService.getObjFromRedis(redisKey, () -> {
            // supplier 函数，查询数据库
            LambdaQueryWrapper<StockWarehouseChangeSettings> queryWrapper = Wrappers
                    .lambdaQuery(StockWarehouseChangeSettings.class)
                    .eq(StockWarehouseChangeSettings::getRecordChannel, recordChannel)
                    .eq(StockWarehouseChangeSettings::getActionType, actionType);

            return getOne(queryWrapper);
        }, StockWarehouseChangeSettings.class);
    }

}




